package com.fivestar.pmanager.service;

import java.util.List;

import com.fivestar.pmanager.core.base.BaseCommonParameterBean;
import com.fivestar.pmanager.core.exception.DMLNoRecordEffectedException;
import com.fivestar.pmanager.core.service.BaseServiceImpl;
import com.fivestar.pmanager.dao.mapper.SysRoleUserMapper;
import com.fivestar.pmanager.model.FsUserBean;
import com.fivestar.pmanager.model.SysRoleBean;
import com.fivestar.pmanager.model.SysRoleUserBean;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

 /**
 * 角色用户关系表
 * SysRoleUserService
 * @author 
 */
@Service("sysRoleUserService")
public class SysRoleUserService extends BaseServiceImpl<SysRoleUserBean>
{
	private final static Logger log = Logger.getLogger(SysRoleUserService.class);
	@Autowired
    private SysRoleUserMapper mapper;


    @Override
    public int insert(SysRoleUserBean sysRoleUserBean, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
    	sysRoleUserBean.initBeforeCreate(commonParam.getActorId());
        int result;
        if ((result = mapper.insert(sysRoleUserBean)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int delete(SysRoleUserBean smRoleUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        smRoleUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.delete(smRoleUser)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }

    @Override
    public int deletePhysics(SysRoleUserBean smRoleUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        int result;
        if ((result = mapper.deletePhysics(smRoleUser)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int update(SysRoleUserBean smRoleUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        smRoleUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.update(smRoleUser)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }
    
    
    @Override
    public int updateBySelective(SysRoleUserBean smRoleUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        smRoleUser.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.updateBySelective(smRoleUser)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public List<SysRoleUserBean> getList(SysRoleUserBean smRoleUser)
    {
        return mapper.getList(smRoleUser);
    }


    @Override
    public SysRoleUserBean getSingleById(Integer arg0)
    {
        return mapper.getSingleById(arg0);
    }


    @Override
    public int getCountOfPage(SysRoleUserBean smRoleUser)
    {
        return mapper.getCountOfPage(smRoleUser);
    }


    @Override
    public List<SysRoleUserBean> getListOfPage(SysRoleUserBean smRoleUser)
    {
        return mapper.getListOfPage(smRoleUser);
    }


	@Override
    public int getCount(SysRoleUserBean smRoleUser)
    {
        return mapper.getCount(smRoleUser);
    }


    @Override
    public void save(SysRoleUserBean smRoleUser, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        if(smRoleUser.getRowId() == null)
        {
            insert(smRoleUser, commonParam);
        }
        else
        {
            updateBySelective(smRoleUser, commonParam);
        }
    }


    @Override
    public SysRoleUserBean getSingle(SysRoleUserBean smRoleUser)
    {
        return mapper.getSingle(smRoleUser);
    }

    /**
     * 根据用户id获取用户拥有的角色
     *
     * @param userId 用户id
     * @return List<SmRoleBean>
     */
    public List<SysRoleBean> findUserRole(@Param("userId")int userId)
    {
        return mapper.findUserRole(userId);
    }
    
    /**
     * 根据角色id获取所有的用户
     *
     * @param roleId 角色id
     * @return List<MdUserBean>
     */
    public List<FsUserBean> findRoleUser(@Param("roleId")int roleId)
    {
        return mapper.findRoleUser(roleId);
    }
    
    /**
     * 根据角色id获取所有的用户Id
     *
     * @param roleId 角色id
     * @return List<Integer>
     */
    public List<Integer> findRoleUserIds(@Param("roleId")int roleId)
    {
        return mapper.findRoleUserIds(roleId);
    }

    /**
     * 根据用户id获取用户拥有的角色id集合
     * @param userId
     * @return
     */
    public String getRoles(int userId)
    {
        return mapper.getRoles(userId);
    }

    public int deleteBySelective(SysRoleUserBean sysRoleUserBean, BaseCommonParameterBean commonParam) {
        return mapper.deleteBySelective(sysRoleUserBean);
    }
}
